#!/usr/bin/env perl
package Tower::API;
use 5.018;
use warnings;
use Mojo::UserAgent;
use Encode qw(encode_utf8);

my $ua = Mojo::UserAgent->new;

my $tower_api = "https://beta1.tower.im/api/v2";
my $tower_team_members_file_name = "$ENV{HOME_UP}/.config/tower_team_members";

use Mojo::JSON qw(true false);
use DTask::Utils qw(wrapUATX);
use Data::Dumper;

# read config from file
use Config::Tiny;
my $secret_config = Config::Tiny->read("$ENV{HOME}/.config/tower_api_secret.cfg");

my $client_id     = $secret_config->{_}{client_id};
my $client_secret = $secret_config->{_}{client_secret};
my $team_guid = $secret_config->{_}{team_guid};

my $access_token;
# functions

sub getAccessToken
{
	my ($username, $password, $cb) = @_;
	my $tower_api_with_client = $tower_api =~ s(https://)(https://$client_id:$client_secret\@)r;
    $ua->post(
        "$tower_api_with_client/oauth/token" => form => {
            grant_type => "password",
            username   => $username,
            password   => $password,
        } ,
	sub {
		my $ret = wrapUATX(@_);
		if ($ret->{error})
		{
			$cb->($ret);
		}
		else
		{

			$ret->{result} = {
				access_token => $ret->{result}{access_token},
				expires_in => $ret->{result}{expires_in},
			};
			$cb->($ret);
		}
	});
}

sub getTeamMembers
{
	my ($access_token, $cb) = @_;
    $ua->get( "$tower_api/teams/$team_guid" =>
		{ Authorization => "Bearer $access_token" } , sub {
		my $ret = wrapUATX(@_);
		warn Dumper $ret;
		if ($ret->{error})
		{
			$cb->($ret);
		}
		else
		{
			my $team_guid_json = $ret->{result};
			my $team_members;
			for ( @{ $team_guid_json->{members} } ) {
				my $guid = $_->{guid};
				my $nickname = $_->{nickname};
				$team_members->{$guid} = $nickname;
			}
			$ret->{result} = $team_members;
			$cb->($ret);
		}
	});

}


sub getProjects
{

	my ($access_token, $cb) = @_;
	$ua->get( "$tower_api/teams/$team_guid/projects" =>
		{ Authorization => "Bearer $access_token" } , sub {
		my $ret = wrapUATX(@_);
		if ($ret->{error})
		{
			$cb->($ret);
		}
		else
		{
			my $projects_json = $ret->{result};
			my $projects;
			for ( @$projects_json )
			{
				my $name = $_->{name};
				my $guid = $_->{guid};
				$projects->{$guid} = $name;
			}
			$ret->{result} = $projects;
			$cb->($ret);
		}
	});
}


sub getTodoLists
{
	my ($access_token, $project_guid, $cb) = @_;
    $ua->get( "$tower_api/projects/$project_guid/todolists" =>
		{ Authorization => "Bearer $access_token" }, sub {
		my $ret = wrapUATX(@_);
		if ($ret->{error})
		{
			$cb->($ret);
		}
		else
		{
			my $todolists;
			my $todolists_json = $ret->{result};
			for my $todolist ( @{ $todolists_json } ) {
				my ($guid , $name ) = @$todolist{qw(guid name)};
				$todolists->{$guid} = $name;
			}
			$ret->{result} = $todolists;
			$cb->($ret);
		}
	});
}

sub getTodos
{
	my ($access_token, $list_guid, $cb) = @_;
    $ua->get( "$tower_api/todolists/$list_guid/todos" =>
		{ Authorization => "Bearer $access_token" } , sub {
		my $ret = wrapUATX(@_);
		if ($ret->{error})
		{
			$cb->($ret);
		}
		else
		{
			my $todos;
			my $todos_json = $ret->{result};
			for ( @$todos_json)
			{
				my $guid = $_->{guid};
				my $content = encode_utf8($_->{content});
				$todos->{$guid} = $content;
			}
			$ret->{result} = $todos;
			$cb->($ret);
		}
	});
}

sub getTodo
{
	my ($access_token, $todo_guid, $cb) = @_;
	my $url = "$tower_api/todos/$todo_guid";
    $ua->get( $url =>
          { Authorization => "Bearer $access_token" } , sub {
		
		my $ret = wrapUATX(@_);
		$cb->($ret);
	});
}


#创建任务清单
sub addTodoList
{
	warn "add todolist";
	my ($project_guid, $title, $cb) = @_;
	$ua->post( "$tower_api/projects/$project_guid/todolists" =>
		{ Authorization => "Bearer $access_token" } => form =>
		{ title => $title } , sub {
		$cb->(wrapUATX(@_));	
	});
}


#创建任务
sub addTodo
{
	my ($access_token, $todolist_guid, $content, $assignee_guid, $due_at, $cb) = @_;
	$assignee_guid //= "";
	$due_at //= "";
	warn "add todo";
	$ua->post("$tower_api/todolists/$todolist_guid/todos" =>
          { Authorization => "Bearer $access_token" } => form =>
		  {
			content => $content,
			assignee_guid => $assignee_guid,
			due_at => $due_at,
		  },
	sub {
		$cb->(wrapUATX(@_));
	});
		
}

#创建评论 任务评论
sub addTodoComment
{
	my ($access_token, $todo_guid, $content, $cb) = @_;
	$content //= '(no content)';
	warn "create todo comment at todo $todo_guid";
	$ua->post("$tower_api/todos/$todo_guid/comments" =>
          { Authorization => "Bearer $access_token" } => form =>
		  {
			content => $content,
		  },
	sub {
		$cb->(wrapUATX(@_));
	});

}

#创建评论 任务清单评论
sub addTodoListComment
{
	my ($todolist_guid, $content, $cb) = @_;
	say "create todolist comment";
	$ua->post("$tower_api/todolists/$todolist_guid/comments" =>
          { Authorization => "Bearer $access_token" } => form =>
		  {
			content => $content,
		  } ,
	sub {
		$cb->(wrapUATX(@_));
	});

}

1;
